package com.jimet.server;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Vector;

import com.jimet.authorization.JimetAuthorization;
import com.jimet.authorization.JimetAuthorizations;
import com.jimet.db.JimetServerUserDB;

public class JimetServerUser {

	
	protected String remoteHost;
	protected String username;
	protected String usernameAlt;
	protected Timestamp createDate;
	protected boolean isTimeStamped;
	protected String passwordMD5;
	protected int timeStampID;
	
	protected int userId = -1;
	
	protected JimetAuthorizations juserAuths;


	public JimetServerUser() {
		this.username = "";
	}
	
	/**
	 * constructs a jimetserveruser with the given id
	 * @param userId
	 * @throws Exception 
	 */
	public JimetServerUser(int userId) throws Exception {
		this.userId = userId;
		fillUserAuthorizations();
	}
	
	
	/**
	 * sets all the params for a constructed jimetServerUser
	 * @param userId
	 * @throws Exception
	 */
	public void load(int userId) throws Exception
	{
		JimetServerUserDB jimdb = new JimetServerUserDB();	

		ResultSet rs = jimdb.getUserById(userId);
		while(rs.next())
		{
			setUserId(rs.getInt(1));
			setUsername(rs.getString(2));
			setPasswordMD5(rs.getString(3));
			setCreateDate(rs.getTimestamp(4));
			setTimeStamped(rs.getBoolean(5));
			setTimeStampID(rs.getInt(6));
			setUsernameAlt(rs.getString(7));
		}
		
		jimdb.disconnect();
	
	}

	/**
	 * @throws Exception
	 * loads all the authorizations for the user including all parent group �ds from user to the top level group
	 */
	public void fillUserAuthorizations() throws Exception
	{
		this.juserAuths = new JimetAuthorizations();
		JimetServerUserDB jsdb = new JimetServerUserDB();
		ResultSet rs = jsdb.getUserAuthorizationSet(this.getUserId());
		
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.load(rs.getString(1));
			this.juserAuths.addAuth(jauth);
			
		}
		jsdb.disconnect();
		JimetServerUserDB jsdb2 = new JimetServerUserDB();
		
		ResultSet rs2 = jsdb2.getUserParentsSet(this.getUserId());
		Vector<String> userParentIds = new Vector<String>();
		while (rs2.next()) 
			userParentIds.add(String.valueOf(rs2.getInt(1)));
		jsdb2.disconnect();
		for (int i=0;i<userParentIds.size();i++){
			int parentId = Integer.parseInt( (String)userParentIds.get(i));
			JimetSupervisorDB jsdb3 = new JimetSupervisorDB();
			fillGroupAuthorizations(parentId,jsdb3);
			jsdb3.disconnect();
		}
	}

	/**
	 * @throws Exception
	 * loads all the authorizations just for the user not applies the inheritance
	 */
	public void fillUserAuthorizationsDirectly() throws Exception
	{
		this.juserAuths = new JimetAuthorizations();
		JimetServerUserDB jsdb = new JimetServerUserDB();
		ResultSet rs = jsdb.getUserAuthorizationSet(this.getUserId());
		
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.load(rs.getString(1));
			this.juserAuths.addAuth(jauth);
			
		}
		jsdb.disconnect();
	}
	
	/**fills the authVec for groups and all parents
	 * @param groupId -  group id to fill this groups authorizations
	 * to initialize the All parents of this group
	 * @throws Exception
	 * bu metodu gruplar dbsine alal�m
	 */
	public void fillGroupAuthorizations(int groupId,JimetSupervisorDB jsdb) throws Exception
	{
		if (this.juserAuths == null)
			this.juserAuths = new JimetAuthorizations();
		
		ResultSet rs = jsdb.getGroupAuthorizationSet(groupId);
		
		while(rs.next()) 
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.load(rs.getString(2));
			this.juserAuths.addAuth(jauth);
		}
		
		rs = jsdb.getGroupParentsSet(groupId);
		while(rs.next())
		{
			fillGroupAuthorizations(rs.getInt(1),jsdb);
		}
	
	}

	
	public boolean isAuthorized(String shortCode) throws Exception
	{
		if(this.juserAuths == null)
			this.fillUserAuthorizations();
		if(!(this.juserAuths == null)) 
		{
			if(this.juserAuths.hasThisAuth(shortCode))
				return true;
		}
			return false;
	}
	
	public boolean isAuthorized(int rightID) throws Exception
	{
		if(this.juserAuths == null)
			this.fillUserAuthorizations();
		
		if(!(this.juserAuths == null)) 
		{
			if(this.juserAuths.hasThisAuth(rightID))
				return true;
			
		}
				
			return false;
	}

	
	/** Logs the action to the repository database. 
	 * Using the supplied input parameters as action detail.
	 * @param actionID - ID of the action to be logged.
	 * @param actionParams - Parameter for the action to be logged.
	 * @throws Exception
	 */
	public void logAction(int actionID,String actionParams) throws Exception
	{
		JimetSupervisorDB jsdb = new JimetSupervisorDB();

		if(actionParams==null)
			actionParams = "";
		jsdb.insertAction(this.getUserId(),this.getRemoteHost(),actionID,actionParams);
		
		jsdb.disconnect();
	}

	/** Logs the action to the repository database. 
	 * Using "" string as action parameter.
	 * @param actionID - ID of the action to be logged.
	 * @throws Exception
	 */
	public void logAction(int actionID) throws Exception
	{
		logAction(actionID,"");
	}

	/** Logs the action to the repository database. 
	 * Using the supplied input parameters as action detail.
	 * @param actionID - ID of the action to be logged.
	 * @param actionParams - Parameter for the action to be logged.
	 * @throws Exception
	 */
	public void logAction(String shortCode,String actionParams) throws Exception
	{
		JimetSupervisorDB jsdb = new JimetSupervisorDB();

		int actionID = jsdb.getActionID(shortCode);
		if(actionParams==null)
			actionParams = "";
		jsdb.insertAction(this.getUserId(),this.getRemoteHost(),actionID,actionParams);
		
		jsdb.disconnect();
	}
	
	
	public void giveUserRight(int authId) throws Exception
	{
		JimetSupervisorDB jsdb = new JimetSupervisorDB();
		jsdb.giveUserRight(this.getUserId(),authId);
		jsdb.disconnect();
	}
	
	public void giveUserRightBD(int authId) throws Exception
	{
		JimetSupervisorDB jsdb = new JimetSupervisorDB();
		jsdb.giveUserRightBD(this.getUserId(),authId);
		jsdb.disconnect();
	}
	
	public void giveUserRight(String authName) throws Exception
	{
		JimetSupervisorDB jsdb = new JimetSupervisorDB();
		jsdb.giveUserRight(this.getUserId(),authName);
		jsdb.disconnect();
	}
	
	public void giveUserRight(int userId , String authName) throws Exception
	{
		JimetSupervisorDB jsdb = new JimetSupervisorDB();
		jsdb.giveUserRight(userId,authName);
		jsdb.disconnect();
	}
	public void removeUserRight(int authId) throws Exception
	{
		JimetSupervisorDB jsdb = new JimetSupervisorDB();
		jsdb.removeUserRight(this.getUserId(),authId);
		jsdb.disconnect();
	}

	public void removeUserRight(String authName) throws Exception
	{
		JimetSupervisorDB jsdb = new JimetSupervisorDB();
		jsdb.removeUserRight(this.getUserId(),authName);
		jsdb.disconnect();
	}
	
	/** Logs the action to the repository database. 
	 * Using "" string as action parameter.
	 * @param actionID - ID of the action to be logged.
	 * @throws Exception
	 */
	public void logAction(String shortCode) throws Exception
	{
		logAction(shortCode,"");
	}
	


	/**
	 * @return Returns the remoteHost.
	 */
	public String getRemoteHost() {
		return remoteHost;
	}



	/**
	 * @param remoteHost The remoteHost to set.
	 */
	public void setRemoteHost(String remoteHost) {
		this.remoteHost = remoteHost;
	}



	/**
	 * @return Returns the createDate.
	 */
	public Timestamp getCreateDate() {
		return createDate;
	}

	/**
	 * @return Returns the passwordMD5.
	 */
	public String getPasswordMD5() {
		return passwordMD5;
	}

	/**
	 * @return Returns the timeStampID.
	 */
	public int getTimeStampID() {
		return timeStampID;
	}

	
	/**
	 * @return Returns the username.
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @return Returns the isTimeStamped.
	 */
	public boolean isTimeStamped() {
		return isTimeStamped;
	}


	/**
	 * @param createDate The createDate to set.
	 */
	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	/**
	 * @param passwordMD5 The passwordMD5 to set.
	 */
	public void setPasswordMD5(String passwordMD5) {
		this.passwordMD5 = passwordMD5;
	}

	/**
	 * @param isTimeStamped The isTimeStamped to set.
	 */
	public void setTimeStamped(boolean isTimeStamped) {
		this.isTimeStamped = isTimeStamped;
	}

	/**
	 * @param timeStampID The timeStampID to set.
	 */
	public void setTimeStampID(int timeStampID) {
		this.timeStampID = timeStampID;
	}

	/**
	 * @param username The username to set.
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return Returns the userId.
	 */
	public int getUserId() {
		return userId;
	}

	/**
	 * @param userId The userId to set.
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}

	/**
	 * @return Returns the juserAuths.
	 */
	public JimetAuthorizations getJuserAuths() {
		return juserAuths;
	}

	/**
	 * @param juserAuths The juserAuths to set.
	 */
	public void setJuserAuths(JimetAuthorizations juserAuths) {
		this.juserAuths = juserAuths;
	}

	/**
	 * @return the usernameAlt
	 */
	public String getUsernameAlt() {
		return usernameAlt;
	}

	/**
	 * @param usernameAlt the usernameAlt to set
	 */
	public void setUsernameAlt(String usernameAlt) {
		this.usernameAlt = usernameAlt;
	}

}
